package com.kim.rssreader.activity;

import java.util.List;

import com.kim.rssreader.R;
import com.kim.rssreader.domain.AbstractDomain;
import com.kim.rssreader.domain.RssCategory;
import com.kim.rssreader.utils.DigestUtils;
import com.kim.rssreader.utils.ValueConstant;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SubscribeCategoryActivity extends BaseActivity implements OnClickListener{
	private ListView categotyList;
	private ImageView img_back;
	private List<AbstractDomain> columnCategory;
	private Boolean isFrequent = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	public void initView() {
		setContentView(R.layout.subscribecategory_layout);
		
		initData();
		
		categotyList = (ListView) findViewById(R.id.sub_category_lv_rss_category_list);
		img_back =(ImageView) findViewById(R.id.sub_category_iv_back);
		columnCategoryAdapter adapter = new columnCategoryAdapter(this,
				columnCategory);
		categotyList.setAdapter(adapter);
		
		img_back.setOnClickListener(this);
		
		categotyList.setOnItemClickListener(new ListView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				//传递类别ID，跳转到栏目订阅界面
				if (DigestUtils.shaHex(ValueConstant.COLUMN_ALL).equals(((RssCategory)columnCategory.get(arg2)).rssCategoryId)) {
					isFrequent = true;
				}else{
					isFrequent = false;
				}
				
				Intent intent = new Intent( SubscribeCategoryActivity.this, SubscribeColumnActivity.class);
				Bundle bundle = new Bundle();
				bundle.putBoolean("isFrequent", isFrequent);
				bundle.putString("categoryId", ((RssCategory)columnCategory.get(arg2)).rssCategoryId);
				bundle.putString("CategoryName", ((RssCategory)columnCategory.get(arg2)).rssCategoryName);
				intent.putExtra("ColumnInfo", bundle);
				
				startActivity(intent);
			}
		});

	}
	
	@Override
	public void onClick(View v) {
		int vid = v.getId();
		switch(vid){
		case R.id.sub_category_iv_back:
			this.finish();
			break;
		}
	}
	

	@Override
	public void initParam() {

	}

	// 获取数据
	private void initData() {
		columnCategory = dbDAO.getRssCategory();

	}

	
	
	
	
	class columnCategoryAdapter extends BaseAdapter {
		private Context context;
		private List<AbstractDomain> categoryList;
		private LayoutInflater inflater;
		private RssCategory category = null;

		public columnCategoryAdapter(Context ct, List<AbstractDomain> list) {
			this.context = ct;
			this.categoryList = list;
			this.inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return categoryList.size();
		}

		@Override
		public Object getItem(int position) {
			return categoryList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.categoty_items, parent,
						false);
				holder = new ViewHolder();
				holder.title = (TextView) convertView
						.findViewById(R.id.category_item_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			category = (RssCategory) categoryList.get(position);
			String title = category.rssCategoryName;

			holder.title.setText(title);

			return convertView;
		}

		private class ViewHolder {
			TextView title;
		}

	}



}
